<?php

require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/appconf.php');

class mapi_itrial_photo extends Site {

    function setup() {
        $this->run_modes('add_photo');
        $this->start_mode('add_photo');
        parent::setup();
    }

    function cgiapp_prerun() {
        $this->mapi_object = getModel('MAPI_Base');
        parent::cgiapp_prerun();
    }

    function action_add_photo() {
        $root = $this->param('WWW');
        $upload_status = uploadfiles($root . '/upload/report', 'photo');
        if ($upload_status) {
            foreach ($upload_status as $k => $v) {
                if ($v['code'] === 1) {
                    $upload_status[$k]['original_photo'] = str_replace($root . '/', '', $upload_status[$k]['path']);
                    $upload_status[$k]['thumb'] = str_replace('//', '', showphoto($upload_status[$k]['path'], 100, 100));
                }
            }
        }
        if ($upload_status[0]['code'] == 1) {
            unset($upload_status[0]['code']);
            unset($upload_status[0]['path']);
            //unset($upload_status[0]['src']);
            $upload_status[0]['thumb'] = $this->param('URL') . $upload_status[0]['thumb'];

            return $this->mapi_object->to_json($upload_status);
        } else {
            return $this->mapi_object->to_json(array(), array(), 0, $upload_status[0]['msg']);
        }
    }

}

header("Content-type: text/html; charset=utf-8");
$webapp = new mapi_itrial_photo();
$webapp->run();
?>
